package com.zimmor.mq.rocket_train.transzction;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2023/2/16 10:08
 * @Description 消费事务消息
 * @version: 1.0.0
 */

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建普通消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group6");
        //设置namesrv
        consumer.setNamesrvAddr("192.168.204.136:9876;192.168.204.137;9876");
        //订阅主题，设置tag
        consumer.subscribe("transaction", "tag3");
        //设置消息模式
        consumer.setMessageModel(MessageModel.CLUSTERING);
        //注册消息监听器
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("msg:" + new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //启动消费者
        consumer.start();
    }
}
